
package JET;

import JET.Centers.ControlCenter;
import JET.Centers.Graphics.GraphicsCenter;
import JET.Centers.Scripts.MyScript;
import JET.Centers.Scripts.ScriptsCenter;
import JET.VirtualKeyboard.VKeyboard;
import JET.physics.engine.JETContext;
import JET.physics.interfaces.ProjectTreeNode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.w3c.dom.Element;

/**
 *
 * @author raos
 */
public class Scene implements ProjectTreeNode {

    private ControlCenter CC = ControlCenter.getInstance();
    public Element cached;
    
    static int idCounter = 0;
    private int id = idCounter++;

    private JETContext              context;

    public ArrayList<SceneItem> sceneItemsList;

    public EnvironmentCenter    EC;
    public GraphicsCenter       GC;
    public ScriptsCenter        SC;
    public VKeyboard            vKeyboard;
    private String name;

    public Scene() {
        sceneItemsList = new ArrayList<SceneItem>();

        vKeyboard = new VKeyboard(this);
        setJETContext(new JETContext());
        EC = new EnvironmentCenter(this);
        GC = new GraphicsCenter(this);
        SC = new ScriptsCenter(this);

        setName("Scene_"+id);
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

////////////////////////////////////////////////////////////////////////////////
//                           S C E N E   I T E M S                            //
////////////////////////////////////////////////////////////////////////////////

    public void addSceneItem(SceneItem si) {
        Collection<SceneItem> sceneItemsCol = getSceneItemsListSync();
        synchronized(sceneItemsCol) {
            sceneItemsCol.add(si);
        }
    }

    public void removeSceneItem(SceneItem si) {
        Collection<SceneItem> sceneItemsCol = getSceneItemsListSync();
        synchronized(sceneItemsCol) {
            sceneItemsCol.remove(si);
        }
    }

    public Collection getSceneItemsListSync() {
        return Collections.synchronizedList(sceneItemsList);
    }

////////////////////////////////////////////////////////////////////////////////
//                      V I R T U A L   K E Y B O A R D                       //
////////////////////////////////////////////////////////////////////////////////

    public void resetVirtualKeyboard() {
        vKeyboard = new VKeyboard(this);
    }
    public VKeyboard getVKeyboard() {
        return vKeyboard;
    }
    public JETContext getJETContext() {
        return context;
    }
    public void setJETContext(JETContext e) {
        context = e;
        e.setScene(this);
    }

    @Override
    public String toString() {
        return getName();
    }

////////////////////////////////////////////////////////////////////////////////
//                                C A C H E                                   //
////////////////////////////////////////////////////////////////////////////////

    public void cache() {
        try {
            cached = SceneCacheHelper.createCached(this);
        } catch(Exception e) {e.printStackTrace();}
    }

    public void reloadCached() {
        EC.clear();
        try {
            if (cached!=null)
                SceneCacheHelper.loadCached(this, cached);
        } catch(Exception e) {e.printStackTrace();}
    }

    public void clearCache() {
        cached = null;
    }

////////////////////////////////////////////////////////////////////////////////
//                     P R O J E C T   T R E E   N O D E                      //
////////////////////////////////////////////////////////////////////////////////

    public ScriptsTreeNode scriptsTreeNode = new ScriptsTreeNode();

    public ProjectTreeNode getChild(int index) {
        switch (index) {
            case 0 :
                return scriptsTreeNode;
        }
        return null;}
    public int getChildCount() {
        return 1;
    }
    public boolean isLeaf() {
        return false;
    }
    public int getIndexOfChild(Object child) {
        if (child instanceof ScriptsTreeNode)
            return 0;
        else
            return -1;
    }

    public void resetNodeValue(Object o) {
        if (o instanceof String)
            CC.renameScene(getName(), (String) o);
    }

    public class ScriptsTreeNode implements ProjectTreeNode {

        public ProjectTreeNode getChild(int index) {
            return (ProjectTreeNode) SC.getScript(index);
        }

        public int getChildCount() {
            return SC.getScriptsCount();
        }

        public boolean isLeaf() {
            return SC.getScriptsCount() == 0;
        }

        public int getIndexOfChild(Object child) {
            if (child instanceof MyScript)
                return SC.getScriptIndex((MyScript) child);
            else
                return -1;
        }

        public void resetNodeValue(Object o) {}

        public String toString() {
            return "Scripts";
        }
    }
}